package com.ydtg.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.ydtg.bean.Order;
import com.ydtg.dao.BaseDao;
import com.ydtg.dao.OrderDao;

public class OrderDaoImpl extends BaseDao implements OrderDao{
	public List<Order> getall(Order news) {
		// TODO 自动生成的方法存根
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Order> list = new ArrayList<Order>();
		
		try {
			conn = getconnection();
			String sql = "select id,materiel,number,price,customer,totalprice from order where 1=1";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Order pet  = new Order();
				pet.setId(rs.getInt(1));
				pet.setMateriel(rs.getString(2));
				pet.setNumber(rs.getInt(3));
				pet.setPrice(rs.getInt(4));
				pet.setCustomer(rs.getString(5));
				pet.setTotalprice(rs.getInt(6));
				list.add(pet);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			this.closeAll(conn, stmt, rs);
		}
		
		return list;
	}

	@Override
	public int add(Order pet) {
		
		String psql = "insert into `order`(materiel,number,price,customer,totalprice) "
				+ "values(?,?,?,?,?)";
		Object[] params = {pet.getMateriel(),pet.getNumber(),pet.getPrice(),pet.getCustomer(),pet.getTotalprice()};
		
		int count=this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public int updateByID(Order pet) {
		
		String psql = "	UPDATE order set materiel=?,number=?,price=?,customer=?,totalprice=? where id=?";
		Object[] params = {pet.getMateriel(),pet.getNumber(),pet.getPrice(),pet.getCustomer(),pet.getTotalprice(),pet.getId()};
		int count =  this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public int delByID(int id) {
		String psql = "	delete from order where id=?";
		Object[] params = {id};
		int count = this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public List<Order> getTypefaceList(Order pet) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Order> list = new ArrayList<Order>();
		try {
			conn = getconnection();
			String psql = "select id,materiel,number,price,customer,totalprice from order where 1=1";
			if (pet!=null&&pet.getId()!=0) {
				psql = psql+" and id="+pet.getId();
			}
			if (pet!=null&&pet.getMateriel()!=null&&!"".equals(pet.getMateriel())) {
				psql = psql+" and materiel="+pet.getMateriel();
			}
			if (pet!=null&&pet.getNumber()!=0) {
				psql = psql+" and number="+pet.getNumber();
			}
			if (pet!=null&&pet.getPrice()!=0) {
				psql = psql+" and price="+pet.getPrice();
			}
			if (pet!=null&&pet.getCustomer()!=null&&!"".equals(pet.getCustomer())) {
				psql = psql+" and customer="+pet.getCustomer();
			}
			if (pet!=null&&pet.getTotalprice()!=0) {
				psql = psql+" and totalprice="+pet.getTotalprice();
			}
			stmt = conn.prepareStatement(psql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Order pet1  = new Order();
				pet1.setId(rs.getInt(1));
				pet1.setMateriel(rs.getString(2));
				pet1.setNumber(rs.getInt(3));
				pet1.setPrice(rs.getInt(4));
				pet1.setCustomer(rs.getString(5));
				pet1.setTotalprice(rs.getInt(6));
				list.add(pet1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			closeAll(conn, stmt, null);
		}
		return list;
	}
}
